package cn.labbook.bluetooth.util;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.*;
import com.lowagie.text.Image;
import com.lowagie.text.rtf.RtfWriter2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.util.List;


public class CreateWord {
    //HttpServletRequest request, HttpServletResponse response可以不加这两个参数
    public static void createDocContext(List<Image> imageList, HttpServletRequest request, HttpServletResponse response) throws DocumentException, IOException {
        File file1 = new File("电表二维码.doc");
        judeFileExists(file1);
        Document document = new Document();
        // 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
        RtfWriter2.getInstance(document, new FileOutputStream(file1));
        document.open();
        Paragraph title = new Paragraph("电表二维码");
        //设置标题格式对齐方式
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);
        // 设置 Table 表格
        //设置表格，将图片加到表格中进行方便定位
        Table aTable = new Table(12);
        // 设置每列所占比例
        // 占页面宽度 90%
        aTable.setWidth(100);
        aTable.setBorder(-1);
        aTable.setBorderColor(Color.white);
//        aTable.setAlignment(Element.ALIGN_CENTER);
        aTable.setAlignment(Element.ALIGN_RIGHT);
        Cell cell = aTable.getDefaultCell();
        cell.setBorderColor(Color.white);
        cell.setBorder(-1);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
//        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        // 自动填满
        /*aTable.setAutoFillEmptyCells(true);*/
        //这里是imagelist集合，就是图片字节流的集合，图片从流中去获取放到word中
        for (int i = 0; i < imageList.size(); i++) {
            Image image = imageList.get(i);
            //设置图片等比例缩小
            if(i%3==0){
                image.scalePercent(12);
            }else{
                image.scalePercent(18);
            }
            aTable.addCell(new Cell(image));
        }
        document.add(aTable);
//        for (int i = 0; i < imageList.size(); i++) {
//            //设置图片等比例缩小
//            Paragraph title1 = new Paragraph();
//            Image image = imageList.get(i);
//            Image image1 = imageList.get(i);
//            image.scalePercent(20);
////            image1.scaleAbsolute(20,40);
//            image.setIndentationLeft(2f);
//            image.setAbsolutePosition(50,20);
//            title1.add(image);
//            title1.add(image1);
//            document.add(title1);
//        }
        document.add(new Paragraph("\n"));
        System.out.println("word----success");
        document.close();

        //响应浏览器 返回下载
        response.setContentType("applicaiton/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + "code.doc");
        InputStream is = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        is = new FileInputStream(file1);
        bis = new BufferedInputStream(is);
        os = response.getOutputStream();
        bos = new BufferedOutputStream(os);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        bis.close();
        is.close();
        bos.close();
        os.close();
        //删除本地临时文件
        file1.delete();
    }
    public static void judeFileExists(File file) {
        if (file.exists()) {
            System.out.println("已存在CZ");
        } else {
            System.out.println("不存在BCZ");
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
